﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using FreeGoo.LevelClasses;

namespace FreeGoo.WpfDemo
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class SelectLevelWindow : Window
    {
        private GameWindow _gameWindow;
        
        public SelectLevelWindow()
        {
            InitializeComponent();
        }

        public CampaignHandler Handler { get; set; }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Handler = this.Resources["CampaignHandler"] as CampaignHandler;
            Handler.Engine = null;

            string[] args = Environment.GetCommandLineArgs();

            string campaignFileName = @"Campaigns\Campaigns.xml";
            if (args.Length > 1)
            {
                Handler.BasePath = args[1];
            }

            Handler.LoadFromFile(campaignFileName);
            CampaignListBox.SelectedItem = Handler.Campaigns[0];
            levelListBox.SelectedItem = Handler.Campaigns[0].Levels[0];

            /* WindowState = WindowState.Normal;
            WindowStyle = WindowStyle.None;
            Topmost = true;
            WindowState = WindowState.Maximized;*/
        }

        private void QuitButton_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void HelpButton_Click(object sender, RoutedEventArgs e)
        {
        }

        private void PlayButton_Click(object sender, RoutedEventArgs e)
        {
            Level selectedLevel = levelListBox.SelectedItem as Level;

            PlayLevel(selectedLevel);            
        }

        private void PlayLevel(Level selectedLevel)
        {
            if (selectedLevel != null)
            {
                if (_gameWindow == null)
                {
                    _gameWindow = new GameWindow(Handler, selectedLevel);
                }
                else
                {
                    _gameWindow.GameControl.Engine.CurrentLevel = selectedLevel;
                }

                _gameWindow.WindowState = WindowState.Maximized;
                _gameWindow.ShowDialog();
                _gameWindow = null;
            }
        }

        private void QuickStartButton_Click(object sender, RoutedEventArgs e)
        {
            string[] args = Environment.GetCommandLineArgs();
            if (args.Length > 2)
            {                
                Level level = new Level(Handler.Campaigns[0], args[2]);
                PlayLevel(level);                
            }
        }
    }
}
